Shader "Level4/BRDF/SimpleBRDF" {
	Properties {
		_MainTex("MainTex",2d) = ""{}
		_ViewPower("View Power",float) = 1
		_RampTex("Ramp Texture",2d) = ""{}
	}
	
	CGINCLUDE
		#include "UnityCG.cginc"
		sampler2D _MainTex;
		half _ViewPower;
		sampler2D _RampTex;
		
		struct v2f{
			half4 pos:POSITION;
			half3 normal:TEXCOORD;
			half2 uv:TEXCOORD1;
		};
		
		v2f vert(appdata_base i){
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
			o.normal = i.normal;
			o.uv = i.texcoord;
			return o;
		}
		
		half4 frag(v2f i):COLOR{
			half NdotL = dot(i.normal,_WorldSpaceLightPos0.xyz);
			half NdotE = dot(i.normal,_WorldSpaceCameraPos);
			
			half diff = NdotL * 0.5 + 0.5;
			half2 brdfUV = half2(NdotE * _ViewPower,diff * _ViewPower);
			half3 BRDF = tex2D(_RampTex,brdfUV);
			
			fixed4 c = tex2D(_MainTex,i.uv);
			c.rgb *= BRDF;
			return c;
		}
	ENDCG
	
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		Pass{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			ENDCG
		}
	} 
	FallBack "Diffuse"
}
